package util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Logger;

/**
 * @BelongsProject: jdbc_demo
 * @BelongsPackage: util
 * @Author: Del
 * @CreateTime: 2025-02-26  08:45
 * @Description: TODO
 * @Version: 1.0
 */
public class DbUtil {

    // 数据库连接信息
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/jdbc_test";
    private static final String USER = "root";
    private static final String PASSWORD = "459633";

    private static final java.util.logging.Logger logger = Logger.getLogger(DbUtil.class.getName());

    static {
        try {
            Class.forName(DRIVER);
            logger.info("加载驱动成功");
        } catch (ClassNotFoundException e) {
            logger.severe("加载驱动失败：" + e.getMessage());
        }
    }

    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
            logger.info("获取连接成功");
        } catch (SQLException e) {
            logger.severe("获取连接失败：" + e.getMessage());
        }
        return connection;
    }


    public static void releaseConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
                logger.info("释放连接成功");
            } catch (SQLException e) {
                logger.severe("释放连接失败：" + e.getMessage());
            }
        }
    }
}
